<!--- 
LICENSE INFORMATION:

Copyright 2007, Hem Talreja
 
Licensed under the Apache License, Version 2.0 (the "License"); you may not 
use this file except in compliance with the License. 

You may obtain a copy of the License at 

	http://www.apache.org/licenses/LICENSE-2.0 
	
Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
CONDITIONS OF ANY KIND, either express or implied. See the License for the 
specific language governing permissions and limitations under the License.

VERSION INFORMATION:	(0.1)

 --->
<cfcomponent output="false"	hint="function that loads exception information about a database">

	<cfset Variables.stcExceptions = StructNew() />
	<cfset Variables.strExceptionFile = "" />


	<cffunction name="init" access="public" returntype="Any" output="false">
		<cfargument name="exceptionFile" type="string" required="true" />
		
		<cfset Variables.strExceptionFile = Arguments.exceptionFile />
		
		<cfset loadExceptions() />
		
		<cfreturn this />
	</cffunction>
	
	
	
	<cffunction name="loadExceptions" access="public" returntype="void" output="false">
	
		<cfset var strFileContent = "" />
		
		<cfif FileExists(Variables.strExceptionFile)>
	
			<cffile action="read" file="#Variables.strExceptionFile#" variable="strFileContent" />
		
			<cfif isWDDX(strFileContent)>
				<cfwddx action="wddx2cfml" input="#strFileContent#" output="Variables.stcExceptions" />
			</cfif>
	
		</cfif>
		
	</cffunction>
	
	
	
	<!--- returns the auto number key that is associated to that table --->
	<cffunction name="getTableCounterKey" access="public" returntype="string" output="false">
		<cfargument name="table" type="string" required="true" />
		
		<cfset var strCounterKey = "" />
		
		<cfif isDefined("Variables.stcExceptions.#table#.META.COUNTERKEY")>
			<cfset strCounterKey = Variables.stcExceptions[table]["META"]["COUNTERKEY"]/>
		</cfif>
		
		<cfreturn strCounterKey />
	</cffunction>
	
	
	
	
	<cffunction name="getTablePrimaryKeys" access="public" returntype="string" output="false">
		<cfargument name="table" type="string" required="true" />
		
		<cfset var strPrimaryKey = "" />
		
		<cfif isDefined("Variables.stcExceptions.#table#.META.PRIMARYKEY")>
			<cfset strPrimaryKey = Variables.stcExceptions[table]["META"]["PRIMARYKEY"]/>
		</cfif>
		
		<cfreturn strPrimaryKey />
	</cffunction>
	
	
	
	<!--- return the list of datatypes --->
	<cffunction name="getTableColumnDataType" access="public" returntype="string" output="false">
		<cfargument name="table" type="string" required="true" />
		<cfargument name="column" type="string" required="true" />
		<cfargument name="dataType" type="string" required="true" hint="default data type. This will be returned if no exception is found" />
		
		<cfset var strDataType = Arguments.dataType />
		
		<cfif isDefined("Variables.stcExceptions.#table#.COLUMNS.#column#.data_type")>
			<cfset strDataType = Variables.stcExceptions[table]["COLUMNS"][column]["DATA_TYPE"] />
		<cfelseif Arguments.dataType contains "int identity" OR Arguments.dataType contains "int unsigned">
			<cfset strDataType = "int" />		<!--- This can be of type numeric, if we find other types of identity fields. --->
		<cfelseif Arguments.Column contains "DateTime">
			<cfset strDataType = "timestamp" />		<!--- convert to cf_sql_type --->
		<cfelseif Arguments.Column contains "Date">
			<cfset strDataType = "Date" />			<!---  --->
		<cfelseif Arguments.Column contains "Time">
			<cfset strDataType = "Time" />
		</cfif>
		
		<cfreturn strDataType />
	</cffunction>
	
	
</cfcomponent>